package net.tygzs.minerpg.Listener;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
import java.util.Set;
import net.tygzs.minerpg.Configuration.Item.RPGItem;
import net.tygzs.minerpg.Configuration.Item.RPGItem.RPGItemType;
import net.tygzs.minerpg.Configuration.packet.Packet;
import net.tygzs.minerpg.Main;
import net.tygzs.minerpg.event.MineRPGTimeChangeEvent;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.PlayerDeathEvent;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.AsyncPlayerChatEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;

/**
 *
 * @author thtTNT
 */
public class MineRPGListener implements Listener
{

    @EventHandler
    public void onEntityDamageByEntity(EntityDamageByEntityEvent e)
    {
        if (e.isCancelled())
        {
            return;
        }
        if (e.getDamager().getType() == EntityType.PLAYER)
        {
            Player player = (Player) e.getDamager();
            ItemStack is = player.getItemInHand();
            if (is.getType() == Material.AIR)
            {
                return;
            }
            RPGItem ri = RPGItem.init(is);
            if (ri == null)
            {
                return;
            }
            if (ri.getDamage() != 0)
            {
                e.setDamage(ri.getDamage());
            }
            if (ri.getDur() == 0)
            {
                player.setItemInHand(new ItemStack(Material.AIR));
            } else
            {
                ri.setDur(ri.getDur() - 1);
            }
            player.setItemInHand(RPGItem.BuildItem(ri));
        }
        if (e.getEntity().getType() == EntityType.PLAYER)
        {
            Player player = (Player) e.getEntity();
            Double damage = e.getDamage();
            int FixedArmor = 0;
            Double SectionalArmor = 0.0;
            RPGItem ri;
            ri = RPGItem.init(player.getInventory().getHelmet());
            if (ri != null)
            {
                FixedArmor = FixedArmor + ri.getFixedArmor();
                SectionalArmor = SectionalArmor + ri.getSectionalArmor();
                ri.setDur(ri.getDur() - 1);
                player.getInventory().setHelmet(RPGItem.BuildItem(ri));
            }
            ri = RPGItem.init(player.getInventory().getChestplate());
            if (ri != null)
            {
                FixedArmor = FixedArmor + ri.getFixedArmor();
                SectionalArmor = SectionalArmor + ri.getSectionalArmor();
                ri.setDur(ri.getDur() - 1);
                player.getInventory().setHelmet(RPGItem.BuildItem(ri));
            }
            ri = RPGItem.init(player.getInventory().getLeggings());
            if (ri != null)
            {
                FixedArmor = FixedArmor + ri.getFixedArmor();
                SectionalArmor = SectionalArmor + ri.getSectionalArmor();
                ri.setDur(ri.getDur() - 1);
                player.getInventory().setHelmet(RPGItem.BuildItem(ri));
            }
            ri = RPGItem.init(player.getInventory().getBoots());
            if (ri != null)
            {
                FixedArmor = FixedArmor + ri.getFixedArmor();
                SectionalArmor = SectionalArmor + ri.getSectionalArmor();
                ri.setDur(ri.getDur() - 1);
                player.getInventory().setHelmet(RPGItem.BuildItem(ri));
            }
            damage = damage * (1 - SectionalArmor) - FixedArmor;
            if (damage < 0)
            {
                e.setDamage(0);
            } else
            {
                e.setDamage(damage);
            }
        }
    }

    @EventHandler
    public void onPlayerInteract(PlayerInteractEvent e)
    {
        ItemStack is = e.getPlayer().getItemInHand();
        if (is.getType() == Material.AIR)
        {
            return;
        }
        RPGItem ri = RPGItem.init(is);
        if (ri == null)
        {
            return;
        }
        if (e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK)
        {
            if (ri.getType() == RPGItemType.Helmet)
            {
                ItemStack is1 = e.getPlayer().getInventory().getHelmet();
                e.getPlayer().setItemInHand(is1);
                e.getPlayer().getInventory().setHelmet(is);
                e.setCancelled(true);
            }
            if (ri.getType() == RPGItemType.Chestplate)
            {
                ItemStack is1 = e.getPlayer().getInventory().getChestplate();
                e.getPlayer().setItemInHand(is1);
                e.getPlayer().getInventory().setChestplate(is);
                e.setCancelled(true);
            }
            if (ri.getType() == RPGItemType.Leggings)
            {
                ItemStack is1 = e.getPlayer().getInventory().getLeggings();
                e.getPlayer().setItemInHand(is1);
                e.getPlayer().getInventory().setLeggings(is);
                e.setCancelled(true);
            }
            if (ri.getType() == RPGItemType.Boots)
            {
                ItemStack is1 = e.getPlayer().getInventory().getBoots();
                e.getPlayer().setItemInHand(is1);
                e.getPlayer().getInventory().setBoots(is);
                e.setCancelled(true);
            }
        }
    }

    @EventHandler
    public void onPlayerJoin(PlayerJoinEvent e)
    {
        Set<String> PacketNames = Main.getPluginConfig().getPackets().keySet();
        for (String PacketName : PacketNames)
        {
            Packet packet = Main.getPluginConfig().getPackets().get(PacketName);
            if (packet.isNewPlayerPacket())
            {
                if (packet.getTime(e.getPlayer().getName()) == 0)
                {
                    packet.sendPacket(e.getPlayer());
                    e.getPlayer().sendMessage(ChatColor.GREEN + "系统自动给您新手礼包" + packet.getName());
                }
            }
        }

    }

    @EventHandler
    public void onInventoryClose(InventoryCloseEvent e)
    {
        String InventoryName = e.getInventory().getName();
        if (InventoryName.startsWith("PacketEditer:"))
        {
            String PacketName = InventoryName.substring(InventoryName.indexOf(":") + 1);
            Main.getPluginConfig().getPackets().get(PacketName).getItems().clear();
            for (ItemStack is : e.getInventory().getContents())
            {
                if (is == null)
                {
                    continue;
                }
                if (is.getType() != Material.AIR)
                {
                    Main.getPluginConfig().getPackets().get(PacketName).getItems().add(is);
                }
            }
            e.getPlayer().sendMessage(ChatColor.GREEN + "保存成功!");
        }
    }

    @EventHandler
    public void onPlayerDeath(PlayerDeathEvent e)
    {
        if (e.getEntity().getKiller() != null)
        {
            Main.getPVPConfig().PlayerKilled(e.getEntity().getName(), e.getEntity().getKiller().getName());
        } else
        {
            Main.getPVPConfig().PlayerKilled(e.getEntity().getName(), null);
        }
        if (Main.getPluginConfig().getWorldConfig().isIsDrop())
        {
            DropExecutor(e);
        }

    }

    @EventHandler
    public void onMineRPGTimeChange(MineRPGTimeChangeEvent e)
    {
        if (e.getTime() % 60 == 0)
        {
            for (Player player : Bukkit.getOnlinePlayers())
            {
                Main.getPluginConfig().getEconomy().depositPlayer(player, Main.getPluginConfig().getServerConfig().getMoneyPerMin());
            }
        }
        if (e.getTime() % 3600 == 0)
        {
            Main.getPluginConfig().getPVPConfig().updatarank();
        }
    }

    @EventHandler
    public void onAsyncPlayerChat(AsyncPlayerChatEvent e)
    {
    }

    private void DropExecutor(PlayerDeathEvent e)
    {
        if (!e.getKeepInventory())
        {
            Inventory inv = e.getEntity().getInventory();
            Double drops = Main.getPluginConfig().getWorldConfig().getDefaultWorldInfo(0).getOddsOfDrop();
            e.setKeepInventory(true);
            for (ItemStack is : inv.getContents())
            {
                if (is == null)
                {
                    continue;
                }
                if (Math.random() > drops)
                {
                    inv.remove(is);
                    e.getEntity().getLocation().getWorld().dropItem(e.getEntity().getLocation(), is);
                }
            }
            e.getEntity().sendMessage(ChatColor.GREEN + "目前物品掉落概率:" + (1 - drops) * 100 + "%");
        }
    }
}
